<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function myAjax() {
            const promise = new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);

                xhr.onreadystatechange = function() {
                    console.log('xhr.readyState', xhr.readyState);
                    if (xhr.readyState === 4) {
                        console.log('state：', xhr.status)

                        if ((xhr.status === 200) || xhr.status === 304) {
                            console.log(xhr.responseText)
                            console.log("data", JSON.parse(xhr.responseText))
                            resolve(JSON.parse(xhr.responseText));
                        } else {
                            reject(new Error('请求失败'));
                        }
                    }

                };
                xhr.send(null);

            });
            return promise;
        }
        const url = './data.json';
        myAjax(url).then((data) => {
            console.log("data", data);
        }).catch((err) => {
            console.error('error' + err)
        });
    </script>

</body>

</html>